<?php

/**
 * 大商创退款相关
 * auther孙凯
 * date 20201223 18:00
 */

namespace App\Http\Controllers\Dsc\Model;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Orderreturn extends Model
{
    protected $table = 'order_return';

    /**
     * 退款状态检查
     * @param $PlatOrderNo
     * @return \Illuminate\Http\JsonResponse
     */
    public static function getrefundStatus($PlatOrderNo)
    {
        $exists = DB::connection('mysql2')->table('order_return')->where(['order_sn' => $PlatOrderNo])->exists();
        if ($exists == false) {
            $data['code'] = '10000';
            $data['refundStatus'] = 'JH_07';
            $data['message'] = 'SUCCESS';
            $data['submessage'] = '没有退款';
            return response()->json($data);
        }
        if ($exists == true) {
            $result = DB::connection('mysql2')->table('order_return')->where(['order_sn' => $PlatOrderNo])->first();
            $return_data = checkreturn($result->refound_status, $result->return_status);
            $data['code'] = '10000';
            $data['refundStatus'] = $return_data['refundStatus'];
            $data['message'] = 'SUCCESS';
            $data['refundStatusdescription'] = $return_data['submessage'];
            $data['submessage'] = $return_data['submessage'];
            return response()->json($data);
        }
    }

    public static function getReturnorderList($PageIndex, $PageSize)
    {
        $handle = mysql_cont('order_return');
        $handle = $handle->orderByDesc('ret_id');

        $select = ['return_sn as refundno',
            'order_sn as  platorderno',
            'should_return as totalamount',
            'apply_time as createtime',
            'cause_id',
            'back_shipping_name as logisticname',
            'back_invoice_no as logisticno',
            'user_id',
            'return_time as updatetime',
            'refound_status',
            'return_status',
            'return_type',
            'cause_id as reason',
            'ret_id',
        ];

        $totalcount = $handle->count();
        $handle = $handle->offset($PageIndex)->limit($PageSize);
        $res = $handle->get($select);
        $res = objectToArray($res);

        $arr = [];
        $array_data = [];
        foreach ($res as $item => $value) {
            $product = Returngoods::getretorder_info($value['ret_id']);
            $product=objectToArray($product);
            $arr['refundno'] = $value['refundno'];
            $arr['platorderno'] = $value['platorderno'];
            $arr['totalamount'] = $value['totalamount'];
            $arr['payamount'] = $value['totalamount'];
            $user = Users::getusers($value['user_id']);
            $user=objectToArray($user);
            $arr['buyernick'] = $user['nick_name'];
            $arr['sellernick'] = '美家臻选';
            $arr['refundno'] = $value['refundno'];
            $arr['createtime'] = Get_date($value['createtime']);
            $arr['updatetime'] = Get_date($value['updatetime']);
            $where = ['order_sn' => $value['platorderno']];
            $orderstatus = Orderinfo::get_order_info($where);
            $orderstatus=objectToArray($orderstatus);
            $arr['orderstatus'] = checkStatus($orderstatus['order_status'], $orderstatus['shipping_status'], $orderstatus['pay_status']);
            $status = checkreturn($value['refound_status'], $value['return_status']);
            $arr['refundstatus'] = $status['refundStatus'];
            $arr['goodsstatus'] = 'JH_98';
            $arr['hasgoodsreturn'] = checktrun($value['return_type']);
            $reason = Cause::getcauser_name($value['reason']);
            $arr['reason'] = $reason->cause_name;
            $arr['productnum'] = $product['return_number'];
            $arr['logisticname'] = $value['logisticname'];
            $arr['logisticno'] = $value['logisticno'];
            $retgoods['PlatProductId'] = $product['goods_sn'];
            $retgoods['OuterID'] = $product['goods_sn'];
            $retgoods['PlatProductId'] = $product['goods_sn'];
            $retgoods['ProductName'] = $product['goods_name'];
            $retgoods['RefundAmount'] = $product['refound'];
            $retgoods['Reason'] = $arr['reason'];
            $retgoods['ProductNum'] = $product['return_number'];
            $arr['RefundGoods'] = array($retgoods);
            $array_data[] = $arr;
        }
        $data['TotalCount'] = $totalcount;
        $data['Refunds'] = $array_data;
        $data['IsSuccess'] = true;
        $data['Message'] = 'SUCCESS';
        $data['Code'] = 10000;
        return $data;
    }

}